package com.baichang.taoshunda.service.impl;

import com.baichang.framework.model.Page;
import com.baichang.framework.utils.KidUtils;
import com.baichang.taoshunda.dao.UserRechargeHistoryDao;
import com.baichang.taoshunda.entity.UserRechargeHistory;
import com.baichang.taoshunda.service.UserRechargeHistoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * Created by ${wushenglu} on 2018/1/30.
 */
@Service
public class UserRechargeHistoryServiceImpl implements UserRechargeHistoryService {
    @Autowired
    private UserRechargeHistoryDao userRechargeHistoryDao;

    /**
     * 保存缴费记录
     *
     * @param userId             缴费用户编号
     * @param isDiscount         是否折扣
     * @param payMethod          支付方式
     * @param preferentialAmount 优惠金额
     * @param mobileNo           充值账号
     * @param rechargeAmount     充值金额
     * @param itemId             充值商品编号
     * @param userName           用户名称
     * @param type               缴费类型
     */
    @Override
    public long saveRechargeHistory(int userId, int isDiscount, int payMethod, Double preferentialAmount, String mobileNo, String rechargeAmount, String itemId, String userName, String type) {
        UserRechargeHistory histroty = new UserRechargeHistory();
        histroty.setUserId(userId);
        histroty.setAccount(mobileNo);
        histroty.setAmount(BigDecimal.valueOf(Double.parseDouble(rechargeAmount)));
        histroty.setIfDiscount(isDiscount);
        histroty.setCreated(new Date());
        String payOrderKid = "payOrder" + KidUtils.getKKid();
        histroty.setPayOrderKid(payOrderKid);
        histroty.setOrderNumber(KidUtils.getKKid());
        histroty.setPreferentialAmount(BigDecimal.valueOf(preferentialAmount));
        histroty.setPayMethod(payMethod);
        histroty.setItemId(Integer.parseInt(itemId));
        histroty.setUserName(userName);
        histroty.setRechargeType(type);
        histroty.setStatus(0);
        return userRechargeHistoryDao.save(histroty);
    }

    @Override
    public void updateStatus(int status, String itemName, int userRechargeId) {
        userRechargeHistoryDao.updateStatus(status, itemName, userRechargeId);
    }

    /**
     * 查询生活缴费记录
     *
     * @param name
     * @param telephone
     * @param nowPage
     * @param pageSize  @return
     */
    @Override
    public Page queryUserLife(String name, String telephone, int nowPage, int pageSize) {
        return userRechargeHistoryDao.queryUserLife(name, telephone, nowPage, pageSize);
    }
    /**
     * 查询充值记录
     *
     * @param userId
     * @param type
     * @param nowPage
     * @param pageSize
     * @return
     */
    @Override
    public List<Map<String, Object>> getUserRechargeHistory(int userId, String type, int nowPage, int pageSize) {
        return userRechargeHistoryDao.getUserRechargeHistory(userId,type,nowPage,pageSize);
    }
}
